package com.zhenhunfan;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

/**
 * <p>Title: 简单RPC，一元RPC</p>
 * <p>Description:</p>
 *
 * @author zhanghong
 * @date 2023/7/17
 */
public class HelloGrpcClient1 {
    public static void main(String[] args) {
        //1.创建通信的管道
        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9000)
                .usePlaintext().build();
        try {
            //2.获得代理对象 stub
            HelloServiceGrpc.HelloServiceBlockingStub helloServiceBlockingStub =
                    HelloServiceGrpc.newBlockingStub(managedChannel);
            //3.完成rpc调用
            //3.1 准备参数
            HelloProto.HelloRequest.Builder builder = HelloProto.HelloRequest.newBuilder();
            builder.setName("镇魂帆");
            HelloProto.HelloRequest helloRequest = builder.build();
            //3.2 进行功能rpc调用，获取响应的内容
            HelloProto.HelloResponse helloResponse = helloServiceBlockingStub.hello(helloRequest);
            String result = helloResponse.getResult();
            System.out.println("result:" + result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            managedChannel.shutdown();
        }
    }
}
